﻿// This file is part of Code4Public (http://code4public.codeplex.com)
// Copyright 2011 Sina Iravanian - <sina@sinairv.com>
//
// This source file(s) may be redistributed, altered and customized
// by any means PROVIDING the authors name and all copyright
// notices remain intact.
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED. USE IT AT YOUR OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
// ------------------------------------------------------------------

using System;
using System.Text;
using System.Drawing;
using System.Xml.Linq;
using YAXLib;

namespace Code4Public.Utils
{
    internal class ColorUtils
    {
        public static string ColorTo8CharString(Color color)
        {
            string str = String.Format("{0:X}", color.ToArgb());

            var sb = new StringBuilder();
            for(int i = 0; i < 8 - str.Length; ++i)
            {
                sb.Append("0");
            }

            return sb + str;
        }

        public static string ColorTo6CharString(Color color)
        {
            return ColorTo8CharString(color).Substring(2);
        }

        public static string ColorTo6CharHtmlString(Color color)
        {
            return "#" + ColorTo6CharString(color);
        }

        public static string ColorToRtfTableEntry(Color color)
        {
            return String.Format(@"\red{0}\green{1}\blue{2}", color.R, color.G, color.B);
        }

        public static bool TryParseColor(string strColor, out Color color)
        {
            color = Color.White;

            strColor = strColor.Trim();
            if (strColor.StartsWith("#")) // remove leading # if any
                strColor = strColor.Substring(1);
            
            int n;
            if (Int32.TryParse(strColor, System.Globalization.NumberStyles.HexNumber, null, out n))
            {
                color = Color.FromArgb(n);
                // sets the alpha value to 255
                color = Color.FromArgb(255, color.R, color.G, color.B);
                return true;
            }
            return false;
        }
    }

    internal class ColorSerializer : ICustomSerializer<Color>
    {
        public void SerializeToAttribute(Color objectToSerialize, XAttribute attrToFill)
        {
            attrToFill.Value = ColorUtils.ColorTo6CharHtmlString(objectToSerialize);
        }

        public void SerializeToElement(Color objectToSerialize, XElement elemToFill)
        {
            elemToFill.Value = ColorUtils.ColorTo6CharHtmlString(objectToSerialize);
        }

        public string SerializeToValue(Color objectToSerialize)
        {
            return ColorUtils.ColorTo6CharHtmlString(objectToSerialize);
        }

        public Color DeserializeFromAttribute(XAttribute attrib)
        {
            Color color;
            if (ColorUtils.TryParseColor(attrib.Value, out color))
                return color;

            throw new YAXBadlyFormedInput(attrib.Name.ToString(), attrib.Value);
        }

        public Color DeserializeFromElement(XElement element)
        {
            Color color;
            if (ColorUtils.TryParseColor(element.Value, out color))
                return color;

            throw new YAXBadlyFormedInput(element.Name.ToString(), element.Value);
        }

        public Color DeserializeFromValue(string value)
        {
            Color color;
            if (ColorUtils.TryParseColor(value, out color))
                return color;

            throw new YAXBadlyFormedInput("[SomeValue]", value);
        }
    }


}
